package api

import (
	"community_end/middleware"
	"community_end/model"
	"community_end/utils/errmsg"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
)

func UserRegister(ctx *gin.Context) {
	username := ctx.PostForm("name")
	password := ctx.PostForm("pass")
	email := ctx.PostForm("email")

	var user model.User
	user.Username = username
	user.Password = password
	user.Email = email
	user.Alias = username

	code := model.InsertUser(&user)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetErrMsg(code),
	})
}

func UserLogin(ctx *gin.Context) {
	username := ctx.PostForm("name")
	password := ctx.PostForm("pass")

	//fmt.Println(username)
	//fmt.Println(password)

	code := model.SelectUserByUsername(username, password)
	token, _ := middleware.GetToken(username)
	ctx.JSON(http.StatusOK, gin.H{
		"code":  code,
		"msg":   errmsg.GetErrMsg(code),
		"token": token,
	})
	return
}

func GetUserByUsername(ctx *gin.Context) {
	token := ctx.Request.Header.Get("Authorization")
	userToken, _ := middleware.AuthToken(strings.Split(token, " ")[1])
	user, code := model.SelectUser(userToken.UserName)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"data":    user,
		"current": 1,
		"size":    len(user.Posts),
		"total":   len(user.Posts),
	})
	return
}

func UserLogout(ctx *gin.Context) {
	ctx.JSON(http.StatusOK, gin.H{
		"code": errmsg.SUCCESS,
		"msg":  "退出登入成功",
	})
}

func ValidateRelationship(ctx *gin.Context) {
	userID := ctx.Param("id")
	//fmt.Println(userID)

	token := ctx.Request.Header.Get("Authorization")
	userToken, _ := middleware.AuthToken(strings.Split(token, " ")[1])
	user, _ := model.SelectUser(userToken.UserName)
	convUserID := strconv.Itoa(int(user.ID))

	relate, code := model.SelectRelationship(userID, convUserID)
	if relate.UserID != 0 && relate.FollowID != 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":      code,
			"hasFollow": true,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":      code,
		"hasFollow": false,
	})
	return
}

func RelationshipSubscribe(ctx *gin.Context) {
	userID := ctx.Param("id")

	token := ctx.Request.Header.Get("Authorization")
	userToken, _ := middleware.AuthToken(strings.Split(token, " ")[1])
	user, _ := model.SelectUser(userToken.UserName)
	convUserID := strconv.Itoa(int(user.ID))

	if userID == convUserID {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "自己不能关注自己",
		})
		return
	}

	//fmt.Println(userID)
	//fmt.Println(convUserID)

	code := model.InsertRelationship(userID, convUserID)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    code,
		"message": "关注成功",
	})
	return
}

func RelationshipUnSubscribe(ctx *gin.Context) {
	userID := ctx.Param("id")
	token := ctx.Request.Header.Get("Authorization")
	userToken, _ := middleware.AuthToken(strings.Split(token, " ")[1])
	user, _ := model.SelectUser(userToken.UserName)
	convUserID := strconv.Itoa(int(user.ID))

	convID, _ := strconv.Atoi(userID)
	fmt.Println(userID)
	fmt.Println(convUserID)

	u, _ := model.SelectUserByID(convID)
	u.FollowCount--
	model.SaveUser(u)

	code := model.DeleteRelateByUserIdAndFollowId(userID, convUserID)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    code,
		"message": "取消关注成功",
	})
	return
}

func UserInfo(ctx *gin.Context) {
	token := ctx.Request.Header.Get("Authorization")
	userToken, _ := middleware.AuthToken(strings.Split(token, " ")[1])
	user, code := model.SelectUser(userToken.UserName)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"data":    user,
		"current": 1,
		"size":    len(user.Posts),
		"total":   len(user.Posts),
	})
	return
}
